package com.sandwich.api.controller.v1;

import com.sandwich.api.apibean.v1.CategoryResponse;
import com.sandwich.admin.domain.CategoryEntity;
import com.sandwich.api.apibean.v1.CategoryRequest;
import com.sandwich.admin.service.CategoryService;
import com.sandwich.common.SimpleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @description 分类信息
 * @date 2021-07-28
 */
@RestController("ApiCategoryControllerV1")
@RequestMapping("/api/v1/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询
     * @param pk
     */
    @GetMapping("/find/{pk}")
    public ResponseEntity<?> find(long pk) {
        CategoryEntity categoryEntity = categoryService.find(pk);
        CategoryResponse categoryResponse = new CategoryResponse(categoryEntity);
        return new ResponseEntity<>(new SimpleResponse(Stream.of(categoryResponse).collect(Collectors.toList())), HttpStatus.OK);
    }


    /**
     * 查询
     */
    @GetMapping("/find/all")
    public ResponseEntity<?> findAllCategory(HttpServletRequest request, @RequestHeader HttpHeaders headers) {
        List<CategoryEntity> categoryEntity = categoryService.findAll();
        List<CategoryResponse> categoryResponses = categoryEntity.stream().map(e -> new CategoryResponse(e)).collect(Collectors.toList());
        return new ResponseEntity<>(new SimpleResponse(categoryResponses), HttpStatus.OK);
    }



    /**
     * 新增或编辑
     * @param categoryRequest
     */
    @PostMapping("/save")
    public ResponseEntity<?> save(HttpServletRequest request, @RequestHeader HttpHeaders headers, @RequestBody CategoryRequest categoryRequest) {
        CategoryEntity categoryEntity = categoryService.save(categoryRequest);
        CategoryResponse categoryResponse = new CategoryResponse(categoryEntity);
        return new ResponseEntity<>(new SimpleResponse(Stream.of(categoryResponse).collect(Collectors.toList())), HttpStatus.OK);
    }

}